package etc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

public class SynchronizationParameters {
	static private String propsFileName = "jsimplesync.properties";
	
	static private String
			localPath = "C:\\Temp",
			localUsername = "test",
			localPassword = "test",
			remoteIP = "127.0.0.1",
			remoteUsername = "test",
			remotePassword = "test";

	static private Integer
			localPort = 50000,
			remotePort = 50000;

// <editor-fold defaultstate="collapsed" desc="GET-SETTERS">
	static public String getLocalPath() {
		return localPath;
	}

	static public void setLocalPath(String localPath1) {
		localPath = localPath1;
	}

	static public String getLocalUsername() {
		return localUsername;
	}

	static public void setLocalUsername(String localUsername1) {
		localUsername = localUsername1;
	}

	static public String getLocalPassword() {
		return localPassword;
	}

	static public void setLocalPassword(String localPassword1) {
		localPassword = localPassword1;
	}

	static public String getRemoteIP() {
		return remoteIP;
	}

	static public void setRemoteIP(String remoteIP1) {
		remoteIP = remoteIP1;
	}

	static public String getRemoteUsername() {
		return remoteUsername;
	}

	static public void setRemoteUsername(String remoteUsername1) {
		remoteUsername = remoteUsername1;
	}

	static public String getRemotePassword() {
		return remotePassword;
	}

	static public void setRemotePassword(String remotePassword1) {
		remotePassword = remotePassword1;
	}

	static public Integer getLocalPort() {
		return localPort;
	}

	static public void setLocalPort(Integer localPort1) {
		localPort = localPort1;
	}

	static public Integer getRemotePort() {
		return remotePort;
	}

	static public void setRemotePort(Integer remotePort1) {
		remotePort = remotePort1;
	}
	// </editor-fold>   

	/**
	 * Load synchronization parameters from the default external properties file.
	 * @return true if a properties file was found and loaded properly, false otherwise
	 */
	static public HashMap<String, Object> loadFromPropertiesFile() {
		Properties props = new Properties();
		
		try{
			FileInputStream propsFileInputStream = new FileInputStream( propsFileName );

			props.load( propsFileInputStream );

			if( props.getProperty("LOCAL_PORT") != null ) {
				setLocalPort( Integer.parseInt( props.getProperty("LOCAL_PORT") ) );
			}
			if( props.getProperty("LOCAL_USERNAME") != null ) {
				setLocalUsername( props.getProperty("LOCAL_USERNAME") );
			}
			if( props.getProperty("LOCAL_PASSWORD") != null ) {
				setLocalPassword( props.getProperty("LOCAL_PASSWORD") );
			}
			if( props.getProperty("LOCAL_PATH") != null ) {
				setLocalPath( props.getProperty("LOCAL_PATH") );
			}

			if( props.getProperty("REMOTE_PORT") != null ) {
				setRemotePort( Integer.parseInt( props.getProperty("REMOTE_PORT") ) );
			}
			if( props.getProperty("REMOTE_USERNAME") != null ) {
				setRemoteUsername( props.getProperty("REMOTE_USERNAME") );
			}
			if( props.getProperty("REMOTE_PASSWORD") != null ) {
				setRemotePassword( props.getProperty("REMOTE_PASSWORD") );
			}
			if( props.getProperty("REMOTE_IP") != null ) {
				setRemoteIP( props.getProperty("REMOTE_IP") );
			}

			propsFileInputStream.close();
			propsFileInputStream = null;
		}catch( IOException io ){
			return null;
		}

		return getAsHashMap();
	}

	/**
	 * Save current parameters in internal variables to properties file.
	 * @return true if save operation suceeded, false otherwise
	 */
	static public Boolean saveToPropertiesFile() {
		Properties props = new Properties();

		try{
			props.setProperty( "LOCAL_PORT", getLocalPort().toString() );
			props.setProperty( "LOCAL_PASSWORD", getLocalPassword() );
			props.setProperty( "LOCAL_USERNAME", getLocalUsername() );
			props.setProperty( "LOCAL_PATH", getLocalPath() );

			props.setProperty( "REMOTE_PORT", getRemotePort().toString() );
			props.setProperty( "REMOTE_PASSWORD", getRemotePassword() );
			props.setProperty( "REMOTE_USERNAME", getRemoteUsername() );
			props.setProperty( "REMOTE_IP", getRemoteIP() );

			FileOutputStream propsFileOutputStream = new FileOutputStream( propsFileName );
			props.store(propsFileOutputStream, "JSimpleSync Configuration Parameters");
			propsFileOutputStream.close();
			propsFileOutputStream = null;
		}catch( IOException io ){
			return false;
		}

		return true;
	}

	static public HashMap<String, Object> getAsHashMap() {
		HashMap<String, Object> retmap = new HashMap<String, Object>(9);

		retmap.put( "LOCAL_PORT", getLocalPort() );
		retmap.put( "LOCAL_PASSWORD", getLocalPassword() );
		retmap.put( "LOCAL_USERNAME", getLocalUsername() );
		retmap.put( "LOCAL_PATH", getLocalPath() );

		retmap.put( "REMOTE_IP", getRemoteIP() );
		retmap.put( "REMOTE_USERNAME", getRemoteUsername() );
		retmap.put( "REMOTE_PASSWORD", getRemotePassword() );
		retmap.put( "REMOTE_PORT", getRemotePort() );

		return retmap;
	}

	static public void loadFromHashMap( HashMap<String, Object> settingsMap ) {
		if( settingsMap.containsKey("LOCAL_USERNAME") ) {
			Object temp = settingsMap.get("LOCAL_USERNAME");
			if( temp instanceof String ) setLocalUsername( (String)temp );
		}
		if( settingsMap.containsKey("LOCAL_PASSWORD") ) {
			Object temp = settingsMap.get("LOCAL_PASSWORD");
			if( temp instanceof String ) setLocalPassword( (String)temp );
		}
		if( settingsMap.containsKey("LOCAL_PATH") ) {
			Object temp = settingsMap.get("LOCAL_PATH");
			if( temp instanceof String ) setLocalPath( (String)temp );
		}
		if( settingsMap.containsKey("LOCAL_PORT") ) {
			Object temp = settingsMap.get("LOCAL_PORT");
			if( temp instanceof Integer ) setLocalPort( (Integer)temp );
		}

		if( settingsMap.containsKey("REMOTE_IP") ) {
			Object temp = settingsMap.get("REMOTE_IP");
			if( temp instanceof String ) setRemoteIP( (String)temp );
		}
		if( settingsMap.containsKey("REMOTE_PORT") ) {
			Object temp = settingsMap.get("REMOTE_PORT");
			if( temp instanceof Integer ) setRemotePort( (Integer)temp );
		}
		if( settingsMap.containsKey("REMOTE_USERNAME") ) {
			Object temp = settingsMap.get("REMOTE_USERNAME");
			if( temp instanceof String ) setRemoteUsername( (String)temp );
		}
		if( settingsMap.containsKey("REMOTE_PASSWORD") ) {
			Object temp = settingsMap.get("REMOTE_PASSWORD");
			if( temp instanceof String ) setRemotePassword( (String)temp );
		}
	}
	
}
